package tech.realcpf.duckengine1.sqlparser;

import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.parser.SqlParseException;
import org.apache.calcite.sql.parser.SqlParser;

public class SqlParserTest {
    private static final SqlDialect PG_DIALECT = SqlDialect.DatabaseProduct.POSTGRESQL.getDialect();
    public static void main(String[] args) throws SqlParseException {
        String sql_catalog_1 = "select * from \"default\"";
        SqlParser sqlParser = SqlParser.create(sql_catalog_1);
        SqlNode sqlNode = sqlParser.parseStmt();

        if (sqlNode instanceof SqlSelect sqlSelect) {

            SqlNode sqlFrom = sqlSelect.getFrom();
            SqlIdentifier sqlIdentifier = (SqlIdentifier)sqlFrom;
            SqlNode newFrom = sqlIdentifier.setName(0,"s3://duck/demo1/tenant_1000/user_1000/catalog.csv");
            sqlSelect.setFrom(newFrom);

            System.out.println(sqlIdentifier);

        }
        System.out.println(sqlNode.toSqlString(PG_DIALECT));
    }
}
